package test.wk.net.heartbeat.handler;

import java.io.IOException;

import com.wk.net.heartbeat.handler.HeartbeatHandler;
import com.wk.net.heartbeat.handler.listeners.console.ConsoleAbsenceListener;
import com.wk.net.heartbeat.handler.listeners.console.ConsoleHeartbeatListener;
import com.wk.net.heartbeat.handler.receivers.TcpReceiver;
import com.wk.net.heartbeat.heartbeats.fixed.FixedHeartbeat;
import com.wk.net.heartbeat.heartbeats.fixed.FixedHeartbeatTransferer;



public class DemoHeartbeatHandler_tcp {
	public static void main(String[] args) {
		final int PORT = 8086;
		final int CHECK_PERIOD = 2;// 检查周期（单位：秒）
		final int ABSENCE_TOLERANCE = 3;

		TcpReceiver receiver = new TcpReceiver(PORT);
		try {
			receiver.listen();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}

		HeartbeatHandler<FixedHeartbeat> handler = new HeartbeatHandler<>(new FixedHeartbeatTransferer(), receiver,
				CHECK_PERIOD, ABSENCE_TOLERANCE);
		
		handler.setHeartbeatListener(new ConsoleHeartbeatListener<>(new FixedHeartbeatTransferer()));
		handler.setAbsenceListener(new ConsoleAbsenceListener());
		
		handler.observe("127.0.0.1", true);
		
		handler.start();
	}
}
